home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13692 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  47 lines

  1. Path: crc-news.doc.ca!usenet
  2. From: Slobodan Celenkovic <Celenkovic.Bob@ic.gc.ca>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Errors in constructors
  5. Date: 26 Mar 1996 22:44:12 GMT
  6. Organization: Industry Canada
  7. Message-ID: <4j9rvs$bi7@crc-news.doc.ca>
  8. References: <4is49h$k1o@usenetp1.news.prodigy.com>
  9. NNTP-Posting-Host: bouhadrah.mohamed.bsd001.ic.gc.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; U; 16bit)
  14.  
  15. Here is what I do:
  16. I declare an RC (return code) variable in the module. Every method 
  17. including constartor & destructor sets the RC var indicating success 
  18. (errOk), or failure (errXxxx). Then simply check the RC:
  19.  
  20. // Module MyObject
  21.  
  22. int moRC ;    // return code
  23.  
  24. MyObject::MyObject()    // default constructor
  25. {
  26.   moRC = errOk ;       // init to Ok
  27. // initializaton code
  28.   SomeTable = (char*) malloc( SomeSize );
  29.   if ( SomeTable == NULL ) {
  30.     moRC = errNotEnoughMemory ;
  31.     return;
  32.   }
  33. // more initialization
  34.   SomeFile = fopen("SomeName","r");
  35.   if (SomeFile == NULL) {
  36.     moRC = errno ;
  37.     return;
  38.   }
  39. }
  40.  
  41. ----------
  42.  
  43. main()
  44. { MyObject a ;
  45.  
  46.   if ( moRC != errOk )  { constructor failed }
  47.